import Axios from 'axios';
import { ElMessage } from 'element-plus';


export const baseURL = 'https://api.ifmcs.com:8816';
export const openURL = 'https://api.ifmcs.com';
// export const baseURL = 'http://192.168.20.32:8815';

// 测试环境
//export const baseURL = 'https://test-api.h1dt.com:8818';
const axios = Axios.create({
	baseURL,
	timeout: 20000 // 请求超时 20s
});
// 前置拦截器（发起请求之前的拦截）
axios.interceptors.request.use(
	(response) => {
		response.headers['open_client'] = '$2a$10$ScatIa5GwuDe8ntVwSZRG.DuyeClz3R.Vv7UxWQb5BRPLf0bQK2g6';
		return response;
	},
	(error) => {
		return Promise.reject(error);
	}
);

// 后置拦截器（获取到响应时的拦截）
axios.interceptors.response.use(
	(response) => {
		/**
		 * 根据实际情况来对 response 和 error 做处理
		 */
		return response;
	},
	(error) => {
		if (error.response && error.response.data) {
			const code = error.response.status;
			const msg = error.response.data.message;
			ElMessage.error(`Code: ${code}, Message: ${msg}`);
			console.error(`[Axios Error]`, error.response);
		} else {
			ElMessage.error(`${error}`);
		}
		return Promise.reject(error);
	}
);

export default axios;
